package com.ysd.lis.service.qc.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.qc.QcInstrItem;
import com.ysd.lis.entity.qc.QcInstrItemRule;
import com.ysd.lis.entity.qc.QcMaterial;
import com.ysd.lis.entity.qc.QcMaterialItem;
import com.ysd.lis.mapper.qc.QcInstrItemRuleMapper;
import com.ysd.lis.mapper.qc.QcMaterialItemMapper;
import com.ysd.lis.mapper.qc.QcMaterialMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.qc.QcInstrItemRuleService;
import com.ysd.lis.service.qc.QcMaterialItemResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2021-10-08
 */
@Service
@Slf4j
public class QcInstrItemRuleServiceImpl extends ServiceImpl<QcInstrItemRuleMapper, QcInstrItemRule> implements QcInstrItemRuleService {

    @Autowired
    QcInstrItemRuleMapper qcInstrItemRuleMapper;
    @Autowired
    QcMaterialMapper qcMaterialMapper;
    @Autowired
    QcMaterialItemMapper qcMaterialItemMapper;
    @Autowired
    QcMaterialItemResultService qcMaterialItemResultService;

    @Override
    public Result findInstrItemRuleList(CommonPageDto pageParam) {
        MPJLambdaWrapper<QcInstrItemRule> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(QcInstrItemRule.class)
                .eq(QcInstrItemRule::getPid, pageParam.getPid())
                .eq(QcInstrItemRule::getDelFlag, 0)
                .orderByAsc(QcInstrItemRule::getSeq);
        List<QcInstrItemRule> ruleList = qcInstrItemRuleMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", ruleList);
    }

    @Override
    public Result saveOrUpdateInstrItemRule(QcInstrItem item) {
        List<QcInstrItemRule> instrItemRuleList = item.getInstrItemRuleList();
        boolean b = this.saveOrUpdateBatch(instrItemRuleList);
        if (b) {
            new Thread(() -> {
                //根据  1仪器，2质控项目
                String ruleType = instrItemRuleList.get(0).getPtype();
                String pid = instrItemRuleList.get(0).getPid();
                if ("1".equals(ruleType)) {
                    //查询
                    MPJLambdaWrapper<QcMaterial> lambdaQueryWrapper = new MPJLambdaWrapper<>();
                    lambdaQueryWrapper.selectAll(QcMaterial.class).eq(QcMaterial::getDelFlag, 0);
                    lambdaQueryWrapper.eq(QcMaterial::getInstrId, pid);
                    lambdaQueryWrapper.le(QcMaterial::getEnableDate, new Timestamp(new Date().getTime()));
                    lambdaQueryWrapper.ge(QcMaterial::getValidDate, new Timestamp(new Date().getTime()));
                    lambdaQueryWrapper.and(wrapper -> wrapper.isNull(QcMaterial::getStopDate).or().ge(QcMaterial::getStopDate, new Timestamp(new Date().getTime())));
                    List<QcMaterial> materialList = qcMaterialMapper.selectList(lambdaQueryWrapper);
                    List<String> mIds = materialList.stream().map(QcMaterial::getId).collect(Collectors.toList());
                    if (!mIds.isEmpty()) {
                        MPJLambdaWrapper<QcMaterialItem> queryWrapper = new MPJLambdaWrapper<>();
                        queryWrapper.selectAll(QcMaterialItem.class)
                                .in(QcMaterialItem::getMaterialId, mIds)
                                .eq(QcMaterialItem::getDelFlag, 0);
                        List<QcMaterialItem> itemList = qcMaterialItemMapper.selectList(queryWrapper);
                        for (QcMaterialItem materialItem : itemList) {
                            QcMaterial material = materialList.stream().filter(a -> a.getId().equals(materialItem.getMaterialId())).findFirst().get();
                            qcMaterialItemResultService.QcInstrItemRuleUpdateAsync(materialItem.getId(), materialItem.getQcItemId(), materialItem.getInstrId(), material.getEnableDate(), material.getValidDate());

                        }

                    }

                } else if ("2".equals(ruleType)) {
                    //查询

                    MPJLambdaWrapper<QcMaterialItem> queryWrapper = new MPJLambdaWrapper<>();
                    queryWrapper.selectAll(QcMaterialItem.class)
                            .innerJoin(QcMaterial.class, QcMaterial::getId, QcMaterialItem::getMaterialId)
                            .eq(QcMaterialItem::getQcItemId, pid)
                            .eq(QcMaterialItem::getDelFlag, 0)
                            .eq(QcMaterial::getDelFlag, 0)
                            .eq(QcMaterial::getInstrId, pid)
                            .le(QcMaterial::getEnableDate, new Timestamp(new Date().getTime()))
                            .ge(QcMaterial::getValidDate, new Timestamp(new Date().getTime()))
                            .and(wrapper -> wrapper.isNull(QcMaterial::getStopDate).or().ge(QcMaterial::getStopDate, new Timestamp(new Date().getTime())));
                    List<QcMaterialItem> itemList = qcMaterialItemMapper.selectList(queryWrapper);
                    List<String> mIds = itemList.stream().map(QcMaterialItem::getMaterialId).collect(Collectors.toList());
                    List<QcMaterial> materialList = qcMaterialMapper.selectBatchIds(mIds);
                    for (QcMaterialItem materialItem : itemList) {
                        QcMaterial material = materialList.stream().filter(a -> a.getId().equals(materialItem.getMaterialId())).findFirst().get();
                        qcMaterialItemResultService.QcInstrItemRuleUpdateAsync(materialItem.getId(), materialItem.getQcItemId(), materialItem.getInstrId(), material.getEnableDate(), material.getValidDate());

                    }

                }

            }).start();
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result batchDeleteInstrItemRuleList(QcInstrItem dto) {
        List<QcInstrItemRule> instrItemRuleList = dto.getInstrItemRuleList();
        List<String> ids = instrItemRuleList.stream().map(QcInstrItemRule::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
